$(function () {
    let markid = getUrlString('markid');
    let enroll_id = getUrlString('enroll_id');
    function getInfo () {
        let names="";
        $('.kdname').each(function (index,e) {
            if(names === ""){
                names=""+$(e).val();
            }else{
                names = names+";"+$(e).val();
            }
        });
        let phones="";
        $('.phone').each(function (index,e) {
            if(phones === ""){
                phones=""+$(e).val();
            }else{
                phones = phones+";"+$(e).val();
            }
        });
        let num = $('#num').val();
        if (names === ''){
            laymsg('请输入快递公司');
            return
        }
        if (phones === ''){
            laymsg('请输入快递单号');
            return;
        }

        if (num === ''){
            laymsg('请输入商品数量');
            return
        }
        //所有图片
        let my_imgs = $("#picList").find(".image");
        if (my_imgs.length === 0){
            laymsg('请上传平台寄件凭证');
            return
        }
        let picurls="";
        my_imgs.each(function(index,e){
            if(picurls === ""){
                picurls=""+$(e).attr("src");
            }else{
                picurls = picurls+";"+$(e).attr("src");
            }
        });
        let data = {
            'markid':markid,
            'send_company':names,
            'kd_number':phones,
            'enroll_id':enroll_id,
            'send_platform':picurls,
            'send_num':num
        };
        console.log(data);
        getData('post','/niu/apiv4/niu_send_package.php',data,function (data) {
            console.log(data);
            if (data.resultcode === '0'){
                laymsg(data.msg);
                setTimeout(function () {
                    nativeCommand('dancool://activity=reflashfin');
                },1000)
            } else {
                laymsg(data.msg)
            }
        })
    }

    $('.addKd').click(function () {
        let html = '<div class="userLi userLi50">' +
            '            <span class="userLiName">快递公司</span>' +
            '            <label for="name"></label><input id="name" type="text" class="kdname" placeholder="请输入快递公司">' +
            '        </div>' +
            '        <div class="userLi userLi50">' +
            '            <span class="userLiName">快递单号</span>' +
            '            <label for="phone"></label><input maxlength="30" id="phone" type="text" class="phone" placeholder="请输入快递单号">' +
            '        </div>';
        $('.kdInfo').append(html);
    });

    $('#getInfo').click(function () {
        getInfo()
    });

    $('.upLoad').click(function () {
        uploadpics();
    });

    function uploadpics() {
        //查看已经有的图片数，超过9个不再上传
        var size=$("#picList").children().length;
        var left=10 - size;
        //如果有N小于9个，则可一上传9-N个
        //调用本地接口
        // console.log("left uploadimg=" + left);
        if(left>0){
            getnative("dancool://uploadfilev2="+left+";700");
        }else {
            laymsg('最多上传9张图片')
        }
    }
});

function getnative(actionurl) {
    var u = navigator.userAgent;
    var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
    var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
    if (isiOS) {
        window.webkit.messageHandlers.getnative.postMessage(actionurl);
    }else {
        ikags.getnative(actionurl)
    }
}

function jsnativeresult (aaa,bbb,ccc) {
    if (ccc !== '' || ccc !== null){
        let cccArr = ccc.split(';');
        let text = '';
        for (let i = 0;i < cccArr.length;i++){
            text += '<li>' +
                '            <img class="image" src="'+cccArr[i]+'" alt="">' +
                '            <img class="closeAlert" src="../../res/img/common/delete.png" alt="">' +
                '        </li>';
        }

        $('#picList').prepend(text);
        let size2 = $("#picList").children().length;
        if (size2 >= 10){
            $('#addPic').hide();
        }else {
            $('#addPic').show();
        }
        $('#picList li .closeAlert').click(function () {
            $(this).parent().remove();
            let size = $("#picList").children().length;
            if (size < 10){
                $('#addPic').show();
            }
        })
    }

}
